<?php
namespace Admin\Controller;

//图片上传类
class PicUploadController extends \Think\Controller{
    
	public function index(){
		if(IS_POST){
            $upload           = new \Think\Upload();
            $upload->maxSize  = 10145728 ;  
            $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
            $upload->rootPath = './Public/upload/';
            $upload->savePath = 'admin/';
            $info             = $upload->uploadOne($_FILES['pic']);
            if(!$info){
                echo json_encode(array('msg'=>$upload->getError()));die;
            }else{
                $this->thumb($upload->rootPath.$info['savepath'].$info['savename']);//缩略裁剪
                echo json_encode(array('msg'=>'suc','url'=>$upload->rootPath.$info['savepath'].$info['savename']));die;
            }
		}
	}
	
    //生成做略图
	public function thumb($img, $width = 900, $height = 900){
		$image = new \Think\Image();
        $image->open($img);
        $size = $image->size();
        if($size[0] <= $width && $size[1] <= $height) return;
        $image->thumb($width, $height,\Think\Image::IMAGE_THUMB_CENTER)->save($img);
    }
    
}